﻿using Common.Library.Files;
using Newtonsoft.Json;
using Spider.Analyse;
using Spider.BuildUrl;
using Spider.Entitys;
using Spider.Get;
using Spider.Init;
using Spider.Rules;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace Spider
{
    /// <summary>
    /// 爬虫启动类
    /// </summary>
    public class SpiderStart : SpiderBase
    {
        #region 属性
        /// <summary>
        /// HTML页面解析XPath
        /// </summary>
        IRule rule { get; set; }

        /// <summary>
        /// 页面解析规则
        /// </summary>
        IAnalyse analyse { get; set; }
        #endregion

        #region 构造函数
        /// <summary>
        /// 默认构造函数
        /// </summary>
        public SpiderStart()
        {

        }

        /// <summary>
        /// 老版构造函数
        /// </summary>
        /// <param name="rule"></param>
        /// <param name="analyse"></param>
        public SpiderStart(IRule rule, IAnalyse analyse)
        {
            this.rule = rule;
            this.analyse = analyse;
        }
        #endregion

        /// <summary>
        /// 爬虫启动入口
        /// </summary>
        public void Start()
        {
            if (init != null)
            {
                InitStart();
            }
            else
            {
                throw new Exception("错误，请指定爬虫起始方法");
            }
        }

        #region 抓取省市县 等信息  老版爬虫入口
        public void Start(string url)
        {
            if (rule == null || analyse == null)
            {
                throw new Exception("请初始化");
            }
            string json = FileStreamhelper.ReadFile("D://1.txt");
            var buildUrl = new 省市县BuildUrl();
            string html = GetHTML.Get(url);
            List<ProvinceEntity> entity = analyse.AnalyseStart<ProvinceEntity>(html, rule);
            try
            {
                //List<ProvinceEntity> entity1 = JsonConvert.DeserializeObject<List<ProvinceEntity>>(json);
                //entity = entity1;
            }
            catch (Exception ex)
            {

            }
            rule = new 市级HTML规则();
            int count = 0;
            foreach (var province in entity)
            {
                List<CityEntity> cityList = null;
                //url = buildUrl.CreateUrl(url) + item.url;
                if (!province.IsSpider)
                {
                    province.Url = buildUrl.CreateUrl(url) + province.Url;
                    html = GetHTML.Get(province.Url);
                    analyse = new 市规则解析();
                    cityList = analyse.AnalyseStart<CityEntity>(html, rule);
                    province.CityList = cityList;
                }
                else
                {
                    cityList = province.CityList;
                }
                foreach (var city in cityList)
                {
                    List<CountyEntity> countyList = null;
                    if (!city.IsSpider)
                    {
                        city.Url = buildUrl.CreateUrl(url) + city.Url;
                        html = GetHTML.Get(city.Url);
                        analyse = new 区域规则解析();
                        countyList = analyse.AnalyseStart<CountyEntity>(html, rule);
                        city.CountyList = countyList;
                        city.IsSpider = true; //城市信息抓取完毕
                    }
                    else
                    {
                        countyList = city.CountyList;
                    }
                    foreach (var county in countyList)
                    {
                        List<StreetEntity> streetList = null;
                        if (!county.IsSpider)
                        {
                            county.Url = buildUrl.CreateUrl(province.Url) + county.Url;
                            html = GetHTML.Get(county.Url);
                            analyse = new 街道规则解析();
                            streetList = analyse.AnalyseStart<StreetEntity>(html, rule);
                            county.StreetList = streetList;
                            county.IsSpider = true; //街道信息抓取完毕
                        }
                        else
                        {
                            streetList = county.StreetList;
                        }
                        foreach (var street in streetList)
                        {
                            if (!street.IsSpider)
                            {
                                street.Url = county.Url.Remove(county.Url.LastIndexOf('/'), county.Url.Length - county.Url.LastIndexOf('/')) + "/" + street.Url;
                                html = GetHTML.Get(street.Url);
                                analyse = new 片区解析规则();
                                var areaList = analyse.AnalyseStart<AreaEntity>(html, rule);
                                street.AreaList = areaList;
                                street.IsSpider = true; //片区信息抓取完毕
                                ++count;
                                Console.WriteLine(count);
                                Console.WriteLine($"{ province.ProvinceName } : { city.CityName } : { county.CountyName } : { street.StreetName } : { areaList.Count } 个片区");
                            }
                        }
                    }
                    province.IsSpider = true; //省级 信息抓取完毕
                    //每个市抓取完毕  保存一下进度
                    json = JsonConvert.SerializeObject(entity);
                    FileStreamhelper.Write(@"D://1.txt", json);
                }
                //每个省抓取完毕  保存一下进度
                json = JsonConvert.SerializeObject(entity);
                FileStreamhelper.Write(@"D://1.txt", json);
            }
            json = JsonConvert.SerializeObject(entity);
            FileStreamhelper.Write(@"D://1.txt", json);
        }
        #endregion

    }
}
